package com.shenfa.dao.xml;

import java.sql.SQLException;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;


import com.ibatis.sqlmap.client.SqlMapClient;
import com.shenfa.bean.xml.*;

@SuppressWarnings({ "finally", "unchecked" })
public class MakeXmlDao {
	
	@Autowired
	private SqlMapClient sqlMapClient;
	
	public Map<String,String> wdMap;
	
	public Map<String,String> config_map;
	/**
	 * 模板缓存
	 */
	public Map<String,String> template_map=new HashMap<String,String>();
	
	public Map<String,List<TempArea>> templatearea_map=new HashMap<String,List<TempArea>>();
	
	
	public Map<String,List<RuleF>> ruleF_map=new HashMap<String,List<RuleF>>();
	public Map<String,List<RuleM>> ruleM_map=new HashMap<String,List<RuleM>>();
	
	/**
	 * 读取配置信息
	 * @return
	 */
	public  Map<String,String> getConfig(){
		config_map=new HashMap<String,String>();
		try {
			List<Map<String,String>> list=sqlMapClient.queryForList("makexml.getCofig");
			if(list.size()>0){
				for(Map<String,String> m:list){
					config_map.put(m.get("id"), m.get("val"));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return config_map;
	}

	/**
	 * 获取待处理的产品列表
	 * @param userid
	 * @return
	 */
	public List<Card> getCardList(String userid){
		List<Card> cardlist=new ArrayList<Card>();
		try {
			cardlist=sqlMapClient.queryForList("makexml.getCardList",userid);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return cardlist;
		}
	}
	/**
	 * 获取产品管理的预印纸列表
	 * @param cardid
	 * @return
	 */
	public List<Yyz> getYyzListByCardId(Map<String,String> paramMap){
		List<Yyz> yyzlist=new ArrayList<Yyz>();
		try {
			yyzlist=sqlMapClient.queryForList("makexml.getYyzListByCardId",paramMap);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return yyzlist;
		}
	}
	/**
	 * 获取用户基本数据
	 * @param paramMap
	 * @return
	 */
	public List<UserBase> getUserBaseList(Map<String,String> paramMap){
		List<UserBase> userlist=new ArrayList<UserBase>();
		try {
			userlist=sqlMapClient.queryForList("makexml.getUserBaseList",paramMap);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return userlist;
		}
	}
	/**
	 * 记录xml生产日志
	 * @param paramMap
	 * @return
	 */
	public Object saveLog(Map<String,String> paramMap){
		Object obj = null;
		try {
			obj=sqlMapClient.insert("makexml.insertLog",paramMap);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return obj;
		}
	}
	/**
	 * 记录xml生产日志
	 * @param paramMap
	 * @return
	 */
	public int upLog(Map<String,String> paramMap){
		int obj = -1;
		try {
			obj=sqlMapClient.update("makexml.upLog",paramMap);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return obj;
		}
	}
	
	/**
	 * 按账期和登录用户ID查询XmlLog表
	 * @param paramMap
	 * @return
	 */
	public List<Map<String,String>> queryXmlLog(Map<String,String> paramMap,int start,int limit) {
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		try {
			list = sqlMapClient.queryForList("makexml.getListXmlLog", paramMap, start, limit);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return list;
		}
	}
	public int queryTotleXmlLog(Map<String,String> paramMap) {
		int num;
		try {
			num = (Integer)sqlMapClient.queryForObject("makexml.getTotleXmlLog", paramMap);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return num;
	}
	
	public List<Foldout> getFoldout(Map<String,String> map){
		List<Foldout> list=new ArrayList<Foldout>();
		try {
			list=sqlMapClient.queryForList("makexml.getFoldout",map);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return list;
		}
	}
	
	public List<UserAccinfo> getUserInfo(String acctnbr){
		List<UserAccinfo> list=new ArrayList<UserAccinfo>();
		try {
			list=sqlMapClient.queryForList("makexml.getUserInfo",acctnbr);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return list;
		}
	}
	
	public List<UserAccinfoDetail> getAccinfoDetail(String acctnbr){
		List<UserAccinfoDetail> list=new ArrayList<UserAccinfoDetail>();
		try {
			list=sqlMapClient.queryForList("makexml.getAccinfoDetail",acctnbr);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return list;
		}
	}
	
	
	public UserBuy getUserBuy(String acctnbr){
		UserBuy ub=new UserBuy();
		try {
			ub=(UserBuy)sqlMapClient.queryForObject("makexml.getUserBuy",acctnbr);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return ub;
		}
	}
	
	public List<Debitinfo> getDebitinfo(String acctnbr){
		List<Debitinfo> list=new ArrayList<Debitinfo>();
		try {
			list=sqlMapClient.queryForList("makexml.getDebitinfo",acctnbr);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return list;
		}
	}
	
	public List<PointInfo> getPoint(String acctnbr){
		List<PointInfo> list=new ArrayList<PointInfo>();
		try {
			list=sqlMapClient.queryForList("makexml.getPoint",acctnbr);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return list;
		}
	}
	
	public PointInfo getPoint2(String acctnbr){
		PointInfo pi=new PointInfo();
		try {
			pi=(PointInfo)sqlMapClient.queryForObject("makexml.getPoint2",acctnbr);
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return pi;
		}
	}
	/**
	 * 获取匹配的模板ID
	 * @param map
	 * @return
	 */
	public String getTemplate(Map<String,String> map){
		if(!template_map.containsKey(map.get("card")+"_"+map.get("temptype"))){
			String s="";
			try {
				s=(String)sqlMapClient.queryForObject("makexml.getTemplate",map);
				if(null==s || "".equals(s)){
					return null;
				}
				template_map.put(map.get("card")+"_"+map.get("temptype"),s);
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
		}
		return template_map.get(map.get("card")+"_"+map.get("temptype"));
	}
	/**
	 * 查询对应的模板区域
	 * @param tid
	 * @return
	 */
	public List<TempArea> getTemplateInfo(String tid){
		if(!templatearea_map.containsKey(tid)){
			List<TempArea> list=new ArrayList<TempArea>();
			try {
				list=sqlMapClient.queryForList("makexml.getTemplateInfo",tid);
				templatearea_map.put(tid,list);
			} catch (SQLException e) {
				e.printStackTrace();
				return list;
			}
		}
		return templatearea_map.get(tid);
	}
	/**
	 * 查询所有可用模板
	 * @param 模板id
	 * @param 规则类型0默认1自定义
	 * @param 区域编号
	 * @param 账期
	 * @return
	 */
	public List<Rule> getRule(String tid,String type,String area){
		Map<String,String> param=new HashMap<String,String>();
		param.put("tid", tid);
		param.put("type", type);
		param.put("area", area);
		param.put("billday",BaseParam.PERIOD);
		List<Rule> list=new ArrayList<Rule>();
		try {
			list=sqlMapClient.queryForList("makexml.getRule",param);
		} catch (SQLException e){ 
			e.printStackTrace();
		}finally{
			return list;
		}
	}
	/**
	 * 查询对应的规则
	 * @param rid
	 * @return
	 */
	public List<RuleF> getRuleF(String rid){
		if(!ruleF_map.containsKey(rid)){
			List<RuleF> list=new ArrayList<RuleF>();
			try {
				list=sqlMapClient.queryForList("makexml.getRuleF",rid);
				ruleF_map.put(rid,list);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ruleF_map.get(rid);
	}
	public Fodder getFodder(String fid){
		Map<String,String> param=new HashMap<String,String>();
		param.put("fid",fid);
		param.put("billday",BaseParam.PERIOD);
		Fodder fodder=null;
		try {
			Object obj=sqlMapClient.queryForObject("makexml.getFodder",param);
			if(null!=obj){
				fodder=(Fodder)obj;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			return fodder;
		}
	}
	
	public List<RuleM> getRuleFFList(String rid){
		if(!ruleM_map.containsKey(rid)){
			List<RuleM> list=new ArrayList<RuleM>();
			try {
				list=sqlMapClient.queryForList("makexml.getRuleFF",rid);
				ruleM_map.put(rid,list);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return ruleM_map.get(rid);
	}
	
	public Map<String,String> getWDInfo(){
		if(null==wdMap){
			try {
				wdMap=sqlMapClient.queryForMap("makexml.getWDInfo",null,"i_field_type","s_field");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return wdMap;
	}
	
//	下面的方法都是存储过程
	public int  Proce_bill_update_point(){
		Object obj;
		try {
			obj = sqlMapClient.update("procedure.NBill_update_point");
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
	public int Proce_bill_update_city(){
		Object obj;
		try {
			obj = sqlMapClient.update("procedure.NBill_update_city");
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
		return 0;
	}
}
